iT邦幫忙

2024 iThome 鐵人賽

DAY 18
0
Odoo

前後端整合學習,不只是後端管理系列 第 18

【Day18】 odoo 資料庫串接(三) DELETE

  • 分享至 

  • xImage
  •  

DELETE 請求:刪除維修訂單

我們將添加一個 DELETE 路由,用於刪除維修訂單。

Controller 內容(DELETE 請求):

from odoo import http
from odoo.http import request
import json

class RepairOrderController(http.Controller):

    # DELETE 請求:刪除維修訂單
    @http.route('/repair/orders/<int:order_id>', type='json', auth='public', methods=['DELETE'], csrf=False)
    def delete_repair_order(self, order_id):
        try:
            # 查找訂單
            repair_order = request.env['repair.order'].sudo().browse(order_id)

            if not repair_order.exists():
                return http.Response(
                    json.dumps({'status': 'error', 'message': 'Order not found'}),
                    content_type='application/json'
                )

            # 刪除維修訂單
            repair_order.sudo().unlink()

            return http.Response(
                json.dumps({
                    'status': 'success',
                    'message': 'Repair order deleted successfully',
                    'order_id': order_id
                }),
                content_type='application/json'
            )

        except Exception as e:
            return http.Response(
                json.dumps({'status': 'error', 'message': str(e)}),
                content_type='application/json'
            )

Postman 請求格式(DELETE):

  • 方法DELETE
  • URLhttp://localhost:8069/repair/orders/<order_id>(例如 http://localhost:8069/repair/orders/1
  • Headers
    • Content-Type: application/json

測試指南:

DELETE 請求:

  1. 使用 Postman 發送 DELETE 請求來刪除維修訂單。
  2. 將請求的 URL 設置為 http://localhost:8069/repair/orders/<order_id>,並將 order_id 替換為具體的維修訂單 ID。

回應格式:

  • 成功的 DELETE 請求應返回:
{
  "status": "success",
  "message": "Repair order deleted successfully",
  "order_id": 1
}

上一篇
【Day17】 odoo 資料庫串接(三) PUT
下一篇
【Day19】需求說明
系列文
前後端整合學習,不只是後端管理19
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言